$(function () {
    $('#registerForm').bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            username: {
                message: '用户名验证失败',
                validators: {
                    notEmpty: {
                        message: '账号不能为空！'
                    },
                    stringLength: {
                        min: 6,
                        max: 18,
                        message: '账号长度必须在6到18位之间！'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_]+$/,
                        message: '账号只能包含大写、小写、数字和下划线！'
                    },
                    remote: {
                        url: '/user/validatorUsername',
                        message: '账号已存在！',
                        threshold :  6 ,
                        type: 'GET'
                    }
                }
            },
            nickName: {
                message: '昵称验证失败',
                validators: {
                    notEmpty: {
                        message: '昵称不能为空！'
                    }
                }
            },
            password: {
                message: '密码验证失败',
                validators: {
                    notEmpty: {
                        message: '密码不能为空！'
                    },
                    regexp: {
                        regexp: /^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])|(?=.*[A-Z])(?=.*[a-z])(?=.*[^A-Za-z0-9])|(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9])|(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9])).{8,16}$/,
                        message: '密码长度为8至16位,并且需要包含数字、小写字母、大写字母、符号(至少三种)'
                    },
                    identical: {
                        field: 'confirmPassword',
                        message: '新密码与确认密码不一致！'
                    },
                    different: {
                        field: 'username',
                        message: '不能和账号名相同！'
                    },
                    stringLength: {
                        min: 8,
                        max: 50,
                        message: '密码长度不能少于8位！'
                    }
                }
            },
            confirmPassword: {
                validators: {
                    notEmpty: {
                        message: '确认密码不能为空！'
                    },
                    regexp: {
                        regexp: /^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])|(?=.*[A-Z])(?=.*[a-z])(?=.*[^A-Za-z0-9])|(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9])|(?=.*[a-z])(?=.*[0-9])(?=.*[^A-Za-z0-9])).{8,16}$/,
                        message: '确认密码长度为8至16位,并且需要包含数字、小写字母、大写字母、符号(至少三种)！'
                    },
                    identical: {
                        field: 'password',
                        message: '密码与确认密码不一致！'
                    },
                    stringLength: {
                        min: 8,
                        max: 50,
                        message: '确认密码长度在8-50之间！'
                    }
                }
            },
            email: {
                validators: {
                    notEmpty: {
                        message: '邮箱不能为空！'
                    },
                    emailAddress: {
                        message: '请输入正确的邮件地址如：123@qq.com！'
                    },
                    remote: {
                        url: '/user/validatorEmail',
                        message: '邮箱已存在',
                        delay: 500,
                        type: 'GET'
                    }
                }
            }
        }
    });
});

//注册提交表单数据
$('#registerFormBtn').click(function (event) {
    $('#registerForm').data('bootstrapValidator').validate();
    if ($('#registerForm').data('bootstrapValidator').isValid()) {
        var nickName = $("#nickName").val().trim();
        var username = $("#username").val().trim();
        var password = $("#password").val().trim();
        var email = $("#email").val().trim();
        var jsonStr = {
            "nickName": nickName,
            "username": username,
            "password": password,
            "email": email
        };
        $.ajax({
            url: "/register",
            method: "post",
            data: jsonStr,
            success: function (htm) {
                document.write(htm);
            },
            error: function (e) {
                layer.msg("系统错误", {icon: 2});
            }
        });
    }
});
